<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="layout/default">
<head lang="en">
    <title>Your profile</title>
</head>
<body>
<div class="row" layout:fragment="content">

    <h2 class="indigo-text center" th:text="#{profile.title}">Personal info</h2>

    <div class="row">

        <div class="col m8 s12 offset-m2">
            <img th:src="@{/uploadedPicture}" width="100" height="100"/>
        </div>

        <div class="col s12 center red-text" th:text="${error}" th:if="${error}">
            Error during upload
        </div>

        <form th:action="@{/profile}" method="post" enctype="multipart/form-data" class="col m8 s12 offset-m2">

            <div class="input-field col s6">
                <input type="file" id="file" name="file"/>
            </div>

            <div class="col s6 center">
                <button class="btn indigo waves-effect waves-light" type="submit" name="upload" th:text="#{upload}">
                    Upload
                    <i class="mdi-content-send right"></i>
                </button>
            </div>
        </form>
    </div>

    <form th:action="@{/profile}" th:object="${profileForm}" method="post" class="col m8 s12 offset-m2">

        <div class="row">
            <div class="input-field col s6">
                <input th:field="${profileForm.twitterHandle}" id="twitterHandle" type="text" th:errorclass="invalid"
                       required="required"/>
                <label for="twitterHandle" th:text="#{twitter.handle}">Twitter handle</label>

                <div th:errors="*{twitterHandle}" class="red-text">Error</div>
            </div>
            <div class="input-field col s6">
                <input th:field="${profileForm.email}" id="email" type="email" th:errorclass="invalid"
                       required="required"/>
                <label for="email" th:text="#{email}">Email</label>

                <div th:errors="*{email}" class="red-text">Error</div>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s6">
                <input th:field="${profileForm.birthDate}" id="birthDate" type="text" th:errorclass="invalid"
                       required="required"
                       th:placeholder="${dateFormat}"/>
                <label for="birthDate" th:text="#{birthdate}">Birth Date</label>

                <div th:errors="*{birthDate}" class="red-text">Error</div>
            </div>
        </div>

        <fieldset class="row">
            <legend th:text="#{tastes.legend}">What do you like?</legend>
            <button class="btn teal" type="submit" name="addTaste" th:text="#{add.taste}">Add taste
                <i class="mdi-content-add left"></i>
            </button>

            <div th:errors="*{tastes}" class="red-text">Error</div>

            <div class="row" th:each="row,rowStat : *{tastes}">
                <div class="col s6">
                    <input type="text" th:field="*{tastes[__${rowStat.index}__]}"
                           th:placeholder="#{taste.placeholder}" required="required"/>
                </div>

                <div class="col s6">
                    <button class="btn red" type="submit" name="removeTaste" th:value="${rowStat.index}"
                            th:text="#{remove}">Remove
                        <i class="mdi-action-delete right waves-effect"></i>
                    </button>
                </div>
            </div>
        </fieldset>

        <div class="row s12 center">
            <button class="btn indigo waves-effect waves-light" type="submit" name="save" th:text="#{submit}">Submit
                <i class="mdi-content-send right"></i>
            </button>
        </div>
    </form>
</div>
<script layout:fragment="script">
    $('button').bind('click', function (e) {
        if (e.currentTarget.name === 'save') {
            $(e.currentTarget.form).removeAttr('novalidate');
        } else {
            $(e.currentTarget.form).attr('novalidate', 'novalidate');
        }
    });
</script>
</body>
</html>
